package avicit.bdp.dds.apc.controller;

import avicit.bdp.dds.dispatch.model.WorkerServerModel;
import avicit.bdp.dds.apc.service.PythonResService;
import avicit.bdp.dds.api.service.MonitorService;
import avicit.bdp.dds.common.Constants;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @金航数码科技有限责任公司
 * @作者：liyb
 * @邮箱：liyb@avic-digital.com
 * @创建时间：2021-05-14 10:27
 * @类说明：python包管理
 * @修改记录：
 */
@RestController
@RequestMapping("/api/bdp/dds/apc/pythonres")
@Api(tags = "python包管理接口")
public class PythonResRest {

    private static final Logger logger = LoggerFactory.getLogger(PythonResRest.class);

    @Autowired
    private MonitorService monitorService;
    @Autowired
    private PythonResService pythonResService;

    @ApiOperation(value = "获取worker节点")
    @GetMapping("/getWorkerGroupNodes")
    public ResponseMsg<List<WorkerServerModel>> getWorkerGroupNodes() {
        ResponseMsg<List<WorkerServerModel>> responseMsg = new ResponseMsg<>();
        Collection<WorkerServerModel> nodes = (Collection)monitorService.queryWorker().get(Constants.DATA_LIST);
        List<WorkerServerModel> workerNodeList = new ArrayList<>(nodes);
        responseMsg.setResponseBody(workerNodeList);
        return responseMsg;
    }

    @ApiOperation(value = "在线安装")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "python脚本", name = "pyScript", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "worker节点", name = "address", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "logId", name = "logId", dataType = "String", paramType = "query")
    })
    @GetMapping("/onlineInstall")
    public ResponseMsg<Boolean> onlineInstall(@RequestParam String pyScript, @RequestParam String address,
                                              @RequestParam String logId) throws Exception {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(pythonResService.onlineInstall(pyScript, address, logId));
        return responseMsg;
    }

    @ApiOperation(value = "离线安装")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "资源ID", name = "resourceId", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "worker节点", name = "address", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "logId", name = "logId", dataType = "String", paramType = "query")
    })
    @GetMapping("/offlineInstall")
    public ResponseMsg<Boolean> offlineInstall(@RequestParam String resourceId, @RequestParam String address,
                                               @RequestParam String logId) throws Exception {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(pythonResService.offlineInstall(resourceId, address, logId));
        return responseMsg;
    }

    @ApiOperation(value = "查询python安装日志")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "logId", name = "logId", dataType = "String", paramType = "query")
    })
    @GetMapping("/getResourceLog")
    public ResponseMsg<String> getResourceLog(@RequestParam String logId) throws Exception {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(pythonResService.getResourceLog(logId));
        return responseMsg;
    }

}
